package com.liduanwei.update.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Created by Wei on 17/10/3.
 */
public class FileUtil {
    static public String getParentPath(File file) {

        return file.getParentFile().getAbsolutePath();
    }

    public static void decompress(String srcPath, String dest) throws Exception {
        File file = new File(srcPath);

        if (!file.exists()) {
            throw new RuntimeException(srcPath + " file not found!!");
        }


        ZipFile zf = new ZipFile(file, Charset.forName("GBK"));/*文件名编码问题,MALFORMED, 暂时统一使用gbk编码*/
       /* ZipFile zf;
        try {
            zf = new ZipFile(file);
        } catch (IllegalArgumentException ex) { *//*文件名编码问题,MALFORMED*//*
            ex.printStackTrace();
            zf = new ZipFile(file, Charset.forName("GBK"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }*/

        Enumeration entries = zf.entries();

        ZipEntry entry;

        while (entries.hasMoreElements()) {

            entry = (ZipEntry) entries.nextElement();

            // 表示文件

            String destFile;
            if (dest == null || "".equals(dest)) {
                destFile = entry.getName();
            } else {
                destFile = dest + File.separator + entry.getName();
            }

            if (entry.isDirectory()) {

                String dirPath = destFile;

                File dir = new File(dirPath);

                dir.mkdirs();

            } else {

                PrintUtil.Companion.print("### 解压至目标文件:" + destFile);
                File f = new File(destFile);

                if (!f.exists()) {
                    File parentDir = f.getParentFile();
                    if (parentDir != null) {
                        parentDir.mkdirs();
                    }
                }

                f.createNewFile();

                // 将压缩文件内容写入到这个文件中

                InputStream is = zf.getInputStream(entry);

                FileOutputStream fos = new FileOutputStream(f);


                int count;

                byte[] buf = new byte[8192];

                while ((count = is.read(buf)) != -1) {
                    fos.write(buf, 0, count);
                }

                is.close();
                fos.close();
            }

        }
    }


    public static byte[] readFile(String file) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(file)));
        return readInputStream(bufferedInputStream);
    }


    public static byte[] readInputStream(InputStream inputStream) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] buffer = new byte[1024];
        int len;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        while ((-1 != (len = bufferedInputStream.read(buffer)))) {
            byteArrayOutputStream.write(buffer, 0, len);
        }
        byteArrayOutputStream.close();
        bufferedInputStream.close();
        return byteArrayOutputStream.toByteArray();
    }


    public static void writeFile(String file, byte[] bytes) throws IOException {
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(file)));
        bufferedOutputStream.write(bytes);
        bufferedOutputStream.close();
    }

}
